import { emojiList } from "@/libs/emoji.js";

let Vue

export default {
  install(_Vue) {
    Vue = _Vue

    /**
     *  全局属性或者方法
     */
    // 全屏加载
    Vue.prototype.$showFullScreenLoading = function (timeout = 15000) {
      const loading = this.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      this.fullscreenLoading = loading
      setTimeout(() => {
        loading.close()
      }, timeout)
    }
    // 关闭全屏加载
    Vue.prototype.$closeFullScreenLoading = function () {
      this.fullscreenLoading.close()
    }

    /**
     * 全局过滤器
     */
    // 留言过滤器
    Vue.filter('emojiFilter', function (val) {
      let reg = /@\((.+?)\)/g;
      let arr = val.match(reg);
      if (arr == null) return val;
      let arr1 = [...new Set(arr)];
      arr1.forEach((item) => {
        let a = item.split("");
        let a1 = a.slice(2, a.length - 1);
        let str1 = a1.join("");
        let index = emojiList.emojiTv.indexOf(str1);
        let html = `<span style="background-image: url(https://gitee.com/lovestarboy/figurebed/raw/master/bilibili/tv/${index}.png);width: 28px;height: 28px;background-size: cover;display:inline-block;vertical-align:text-bottom;
    "></span>`;
        try {
          let reg1 = new RegExp("@\\(" + str1 + "\\)", "g");
          val = val.replace(reg1, html);
        } catch (err) {
          console.log(1);
        }
      });
      return val;
    })

    // 时间过滤器
    Vue.filter('dateTimeFilter', function (time, reverse = false, delimiterDate = '/', delimiterTime = ':') {
      if (!time) {
        return '--'
      }
      time = new Date(time)
      const year = time.getFullYear()
      let month = time.getMonth() + 1
      let day = time.getDate()
      let hour = time.getHours()
      let minute = time.getMinutes()
      let second = time.getSeconds()
      month = month < 10 ? '0' + month : month
      day = day < 10 ? '0' + day : day
      hour = hour < 10 ? '0' + hour : hour
      minute = minute < 10 ? '0' + minute : minute
      second = second < 10 ? '0' + second : second
      if (reverse) return `${year}年${month}月${day}日 ${hour}:${minute}:${second}`
      return day + delimiterDate + month + delimiterDate + year + ' ' + hour + delimiterTime + minute + delimiterTime + second
    })

    // 评论时间过滤器
    Vue.filter('commentDateFilter', function (time, option) {
      if (!time) return '--'
      const d = new Date(time)
      const now = Date.now()
      const diff = (now - d) / 1000
      if (diff < 30) {
        return '刚刚'
      } else if (diff < 3600) {
        // less 1 hour
        return Math.ceil(diff / 60) + '分钟前'
      } else if (diff < 3600 * 24) {
        return Math.ceil(diff / 3600) + '小时前'
      } else if (diff < 3600 * 24 * 2) {
        return '1天前'
      }
      if (option) {
        return parseTime(time, option)
      } else {
        return (
          d.getMonth() +
          1 +
          '月' +
          d.getDate() +
          '日' +
          d.getHours() +
          '时' +
          d.getMinutes() +
          '分'
        )
      }
    })

  }
}
